Loops

While

#!/usr/bin/env bash

counter=0
while [ $counter -le 10 ]
do 
    echo $counter
    # counter=$((counter+1))
    # ((counter++)) # returns the value of counter before 
    # ((++counter)) # returns the value of counter after
    ((counter+=1))
    sleep 0.5
done 

Until

#!/usr/bin/env bash

# The until loop, runs until a condition becomes true
# Structure:
# until [ condition ]
# do
    # contents
# done

# Example:

until [ -f "hello.txt" ]
do
    echo "Let's make a file named, hello.txt"
    read -p "What would you like to call the file? " filename
    touch "$filename"  # use the quotes to prevent word splitting in the variable
done

echo "Good Job. You may want to delete any other files you created"

For

#!/usr/bin/env bash

# traditional for loop
for (( i=0 ; i < 5 ; i++ )) # don't forget the spaces
do 
    echo "Counter $i"
done

# for each loops
for i in 1 2 3 4 5
do
    echo "Value: $i"
done 

# A range of values
for i in {0..5}     # no spaces
do 
    echo "Range: $i"
done 

# specify an increment
for i in {0..10..2} # count from 0 to 10 by 2 (bash 4+)
do 
    echo "Inc: $i"
done 

Loop over files

#!/usr/bin/env bash

# looping over files in this directory
for file in *
do
    if [ -f "$file" ]
    then 
        echo $file
    fi
done 

parent_dir=".."
for directory in $parent_dir/*
do
    if [ -d "$directory" ]
    then 
        echo $directory
    fi
done 

absolute_dir="/Users/josh/Desktop"
for directory in $absolute_dir/*
do 
    if [ -d "$directory" ] 
    then
        echo $directory
    fi 
done 

Loop over args

#!/usr/bin/env bash

# recall: to access args use $@

for arg in $@
do 
    echo $arg
done